package com.ganbo.cloud.user.controller;

import com.ganbo.cloud.user.config.PatternProperties;
import com.ganbo.cloud.user.pojo.User;
import com.ganbo.cloud.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

@Slf4j
@RestController
@RequestMapping("/user")
@RefreshScope  //nacos热更新
public class UserController {

    @Autowired
    private UserService userService;

    @Value("${server.port}")
    private Integer port;

    @Value("${spring.cloud.nacos.discovery.cluster-name:''}")
    private String clusterName;

//    @Value("${pattern.dateformat}")
//    private String dateformat;

    @Autowired
    PatternProperties properties;

    @GetMapping("prop")
    public PatternProperties properties(){
        return properties;
    }

    @GetMapping("now")
    public String now() {
        System.out.println("dateformat============>" + properties.getDateformat());
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(properties.getDateformat(), Locale.CANADA));
    }


    /**
     * 路径： /user/110
     *
     * @param id 用户id
     * @return 用户
     */
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id,
                          @RequestHeader(value = "Truth", required = false) String truth) {
        System.out.println("truth: " + truth);
        User user = userService.queryById(id);
        user.setClusterName(clusterName);
        user.setPort(port);
        return user;
    }

}
